#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import tkinter as tk
import os

import subprocess
import traceback

window = tk.Tk()

window.title('Android Scrcpy')

window.geometry('400x420')

# https://blog.csdn.net/ahilll/article/details/81531587

l = tk.Label(window, text='设备列表', font=('Arial', 12), width=30, height=1)
# 说明： bg为背景，font为字体，width为长，height为高，这里的长和高是字符的长和高，比如height=2,就是标签有2个字符这么高
 
# 第5步，放置标签
l.pack()    # Label内容content区域放置位置，自动调节尺寸
# 放置lable的方法有：1）l.pack(); 2)l.place();
var2 = tk.StringVar()
# var2.set((1,2,3,4)) # 为变量var2设置值
list = tk.Listbox(window, width=200, height=9, listvariable=var2)

list.pack()


def findDevices():
	try:
		list.delete(0, "end")
		cmd = "adb devices"
		obj = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		obj.wait()
		lines = obj.stdout.readlines()

		    
		if not lines or len(lines) == 0:
		    lines = obj.stderr.readlines()
		    
		# allLines = lines.splite(",")

		for index in range(len(lines)):
			ll = str(lines[index], "UTF-8").replace("device","").replace("\r\n","")

			if index != 0 and ll:
				print ("Show device: " + ll)
				list.insert(index, ll)

			list.select_set(0,0)
	    
	except Exception:
	    print ("errrrrr")
	else:
	    print ("okkkkk")
		
entery = tk.Entry(window)
entery.pack()
def adbConnect():
	try:
		device_ip = entery.get()
		print ("Device ip: " + device_ip)
		cmd = "adb connect " + device_ip
		# subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		obj = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		
		findDevices()
	except Exception:
	    print ("adb connect send error")
	else:
	    print ("adb connect send success")
		

btn = tk.Button(window, text='连接设备', font=('Arial', 12), width=30, height=2, command = adbConnect)
btn.pack()

def disConnect():
	try:
		cmd = "adb disconnect "
		# subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		obj = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

		findDevices()
	except Exception:
	    print ("adb disconnect send error")
	else:
	    print ("adb disconnect send success")
		
btn = tk.Button(window, text='断开设备', font=('Arial', 12), width=30, height=2, command = disConnect)
btn.pack()

btn = tk.Button(window, text='刷新列表', font=('Arial', 12), width=30, height=2, command = findDevices)
btn.pack()

def connectDevice():
	try:
		device_name = list.get(list.curselection())
		print ("Select device: " + device_name)
		cmd = "scrcpy.exe -s " + device_name
		# subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		obj = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
		# obj.wait()
		# lines = obj.stdout.readlines()
		# print (lines)
	except Exception:
	    print ("Connect device error")
	else:
	    print ("Connect device success")

btn = tk.Button(window, text='远程控制', font=('Arial', 12), width=30, height=2, command = connectDevice)
btn.pack()
findDevices()
# 进入消息循环
window.mainloop()
